<template>
<el-card shadow="never" style="margin: 10px 0;">
  <div>
      <h2>版本更新</h2>
  </div>
  <el-form :model="form">
    <el-row :gutter="30">
      <el-col :xs="24" :sm="12" :md="8" :lg="6">
        <el-form-item label="项目码:">
          <el-input v-model="form.projectCode" placeholder="请输入项目码" />
        </el-form-item>
        <el-form-item label="版本号:">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="一级上游:">
          <el-input v-model="form.up_CIDS" placeholder="请输入一级上游" />
        </el-form-item>
        <el-form-item label="上传文件:">
            <el-upload
            class="upload-demo"
            action="#"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :before-upload="handleBeforeUpload"
            :on-success="handleSuccess1"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            >
                <el-button type="primary" size="mini">上传文件</el-button>
            </el-upload>
          </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <div style="text-align: right;">
    <el-button type="primary" @click="saveData()">确定</el-button>
    <el-button @click="reset()">重置</el-button>
  </div>
</el-card>
  <!-- 新增或编辑的抽屉 -->

</template>

<script>
import sDrawer from "@/components/s-drawer/s-drawer.vue"
export default {
  components: {
    sDrawer,
  },
  watch: {
    visible(value) {
      if (!value) {
        this.form = {}
      }
    }
  },
  data() {
    return {
      form: {
        userid:"",
        username:"",
        projectCode:"",
        version:"",
        up_CIDS:"",
        file:null,
        fileURL:"",

      },
      visible: false,
      tableData: [],
      totalNum: 100,
      search: {
        pageNum: 1,
        pageSize: 10,
        // token: "",
      },
    };
  },
  created() {
    // this.search.token = localStorage.getItem("token");
    // this.getList();
    this.form.userid = localStorage.getItem("user_id");
    this.form.username = localStorage.getItem("username");
  },
  methods: {
      reset() {
        this.form =
            {
              projectCode: "",
              version: "",
              up_CIDS: "",
              file: null,
              fileURL: "",
            };
      },
    editItem(row) {
      this.form = this.$deepClone(row)
      this.visible = true
    },
    async saveData() {
      const res = await this.$request.post('/filePost/contributor/', this.form,{
             headers: {
              'Content-Type': 'multipart/form-data'
            }
          })
          if (res.data.code === 200) {
              this.$message.success(res.data.message)
              this.getList();
              this.visibleAdd = false
            // 在成功保存数据后自动刷新页面
              location.reload();

            }else if (res.data.code === 500) {
              this.$message.error("用户无法进行修改，需要获取创建该项目的项目码");
            }else if(res.data().code === 400){
             this.$message.error("相同CID的文件已经存在！");
            }else if(res.data().code === 401){
             this.$message.error("文件等数据读取错误，请重新登录");
            }
    },


    // 照片文件超出个数限制时的钩子
    handleExceed(files, fileList) {
      this.$notify.warning({
      title: '警告',
      message: `只能选择 ${this.limitNum} 个文件，当前共选择了 ${files.length + fileList.length} 个`
      })
    },
    // 上传照片文件之前的钩子
    handleBeforeUpload(file) {
       console.log(file)
      // 处理文件信息
      this.form.file = file;
      this.form.fileUrl = URL.createObjectURL(file);
      this.$message.success('文件已选择');
    },
    handleSuccess1(res) {
        console.log(res.data,'777777777777777777777777777777777777777')
        this.form.img_url = res.data.file
        this.dialogImageUrl = ''
        this.$notify({
            title: '通知',
            message: '上传文件成功，同时附加信息到区块链上',
            type: 'success',
            duration: 5000
      })
    },



  },
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
}

.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0px;
}
</style>